package com.example.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DatabaseConnectionTest {
    public static void main(String[] args) {
        try {
            // 加载MySQL驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("驱动加载成功");
            
            // 建立连接
                    String url = "jdbc:mysql://47.120.11.196:3306/fafhub?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
        String username = "fafhub";
        String password = "fafhub123";
            Connection connection = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接成功!");
            
            // 执行查询
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SHOW TABLES");
            System.out.println("数据库中的表:");
            while (resultSet.next()) {
                System.out.println("- " + resultSet.getString(1));
            }
            
            // 关闭连接
            resultSet.close();
            statement.close();
            connection.close();
            System.out.println("数据库连接已关闭");
            
        } catch (Exception e) {
            System.err.println("测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 获取用户列表
     */
    public List<Map<String, Object>> getUserList() throws Exception {
        List<Map<String, Object>> results = new ArrayList<>();
        
        // 加载MySQL驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        
        // 建立连接
        String url = "jdbc:mysql://47.120.11.196:3306/fafhub?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true";
        String username = "fafhub";
        String password = "fafhub123";
        
        try (Connection connection = DriverManager.getConnection(url, username, password);
             Statement statement = connection.createStatement()) {
            
            // 执行查询
            ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
            
            // 处理结果集
            while (resultSet.next()) {
                Map<String, Object> user = new HashMap<>();
                
                // 获取表的元数据
                int columnCount = resultSet.getMetaData().getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultSet.getMetaData().getColumnName(i);
                    Object value = resultSet.getObject(i);
                    user.put(columnName, value);
                }
                
                results.add(user);
            }
        }
        
        return results;
    }
} 